package com.iwdnb.gkgz.application.kline;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.google.common.collect.Lists;
import com.iwdnb.gkgz.common.model.dto.StockDayData;
import com.iwdnb.gkgz.common.utils.BigDecimalUtils;
import com.iwdnb.gkgz.common.utils.StockVolumeUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

/**
 * 倒锤子线
 */
@Component
public class InvertHammer implements Shape {

    @Override
    public List<StockDayData> getMatchData(String code, List<StockDayData> list,
        List<String> matchDateList) {
        List<StockDayData> stockDayDataList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            StockDayData stockDayData = list.get(i);
            if (!match(code, stockDayData, Lists.newArrayList(list.subList(0, i)))) {
                continue;
            }
            BigDecimal beforeMaxPrice = getBeforeMaxPrice(list, i);
            if (stockDayData.getMaxPrice().compareTo(beforeMaxPrice) >= 0) {
                stockDayDataList.add(stockDayData);
            }
        }
        return stockDayDataList;
    }

    private BigDecimal getBeforeMaxPrice(List<StockDayData> list, int i) {
        List<StockDayData> subList = null;
        if (i < 10) {
            subList = list.subList(0, i);
        } else {
            subList = list.subList(i - 10, i);
        }
        BigDecimal maxPrice = new BigDecimal(0);
        for (StockDayData stockDayData : subList) {
            if (stockDayData.getMaxPrice().compareTo(maxPrice) > 0) {
                maxPrice = stockDayData.getMaxPrice();
            }
        }
        return maxPrice;
    }

    @Override
    public boolean match(String code, StockDayData data, List<StockDayData> beforeDatas) {
        if (data.getClosePrice().compareTo(data.getMaxPrice()) == 0) {
            return false;
        }
        BigDecimal upPrice = data.getOpenPrice().compareTo(data.getClosePrice()) > 0 ? data.getOpenPrice()
            : data.getClosePrice();
        BigDecimal upLineRange = data.getMaxPrice().subtract(upPrice);
        if (upLineRange.compareTo(BigDecimal.ZERO) == 0) {
            upLineRange = BIGDECIMAL_ONE_PERCENT;
        }
        BigDecimal upRate = BigDecimalUtils.divideToPrecent(upLineRange, upPrice);
        //上影线5个点以上
        if (BigDecimalUtils.isGe(upRate, new BigDecimal(5))) {
            if (CollectionUtils.isNotEmpty(beforeDatas)) {
                BigDecimal tradeNumScale = StockVolumeUtils.getScaleOfBeforeDays(data, beforeDatas);
                return BigDecimalUtils.isGt(tradeNumScale, new BigDecimal(1.6));
            }
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        StockDayData data = new StockDayData();
        String code = "sh600021";
        data.setOpenPrice(new BigDecimal(4.52));
        data.setClosePrice(new BigDecimal(4.48));
        data.setMinPrice(new BigDecimal(4.47));
        data.setMaxPrice(new BigDecimal(4.59));
        InvertHammer hammer = new InvertHammer();
        System.out.println(hammer.match(code, data, null));

    }
}
